import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: '/investManage',
    component: Layout,
    meta: { title: '投顾管理', roles: ['admin'] },
    children: [
      {
        path: '',
        component: () => import('@/views/investManage'),
        name: 'InvestManage',
        meta: {
          title: '投顾管理',
          roles: ['admin'], // or you can only set roles in sub nav
          icon: 'component'
        }
      }
    ]
  },
  {
    path: '/policyManage',
    component: Layout,
    redirect: '/policyManage',
    meta: { title: '策略管理', roles: ['admin'] },
    children: [
      {
        path: '/policyManage',
        component: () => import('@/views/policyManage/index'),
        name: 'PolicyManage',
        meta: { title: '策略管理', roles: ['admin'], icon: 'nested', breadcrumb: false }
      },
      {
        path: 'netWorthAnalysis/:id(\\d+)',
        component: () => import('@/views/policyManage/netWorthAnalysis'),
        name: 'NetWorthAnalysis',
        meta: { title: '净值分析[策略管理]', roles: ['admin'], activeMenu: '/policyManage' },
        hidden: true,
      },
      {
        path: 'contrast/:id(\\d+)',
        component: () => import('@/views/other/contrast'),
        name: 'Contrast',
        meta: { title: '策略对比', roles: ['admin'], activeMenu: '/policyManage' },
        hidden: true,
      }
    ]
  },
  
  {
    path: '/strategyComb',
    component: Layout,
    redirect: '/strategyComb/combManage',
    name: 'StrategyComb',
    meta: { title: '组合管理', icon: 'tree-table', roles: ['admin'] },
    children: [
      {
        path: '/strategyComb/combManage',
        component: () => import('@/views/strategyComb/combManage/combManage'),
        name: 'Combination',
        meta: { title: '策略组合', roles: ['admin'] }
      },
      {
        path: '/strategyComb/combManage/netWorthAnalysis/:id(\\d+)',  // 多开标签
        component: () => import('@/views/strategyComb/combManage/details'),
        name: 'StrategyCombManageDetails',
        meta: {
          title: '净值分析[策略组合]', roles: ['admin'], activeMenu: '/strategyComb/combManage',
          breadCrumb: [
            { path: '/strategyComb', title: '组合管理' },
            { path: 'combManage', title: '策略组合' },
            { path: 'netWorthAnalysis', title: '净值分析' }
          ]
        },
        hidden: true,
      },
      {
        path: '/strategyComb/combManage/config',
        component: () => import('@/views/strategyComb/combManage/config'),
        name: 'StrategyCombManageConfig',
        meta: { 
          title: '配置组合', roles: ['admin'], activeMenu: '/strategyComb/combManage',
          breadCrumb: [
            { path: '/strategyComb', title: '组合管理' },
            { path: 'combManage', title: '策略组合' },
            { path: 'netWorthAnalysis', title: '配置组合' }
          ]
        },
        hidden: true,
      },
      {
        path: '/strategyComb/groupManage',
        component: () => import('@/views/strategyComb/groupManage/groupManage'),
        name: 'GroupManage',
        meta: { 
          title: '策略分类', roles: ['admin'] }
      },
      {
        path: '/strategyComb/groupManage/netWorthAnalysis/:id(\\d+)',
        // path: '/strategyComb/groupManage/netWorthAnalysis',
        component: () => import('@/views/strategyComb/groupManage/details'),
        name: 'StrategyCombDetails',
        meta: { title: '净值分析[策略分类]', roles: ['admin'], activeMenu: '/strategyComb/groupManage' },
        hidden: true,
      },
      {
        path: '/strategyComb/groupManage/relationStrategy',
        component: () => import('@/views/strategyComb/groupManage/relationStrategy'),
        name: 'GroupManageRelationStrategy',
        meta: { title: '关联策略', roles: ['admin'], activeMenu: '/strategyComb/groupManage', noCache: true },
        hidden: true,
      },
      {
        path: '/strategyComb/strategyList',
        component: () => import('@/views/strategyComb/strategyList/strategyList'),
        name: 'StrategyList',
        meta: { 
          title: '策略列表', roles: ['admin'] }
      },
    ]
  },

  {
    path: '/afterManage',
    component: Layout,
    redirect: '/afterManage/afterInvest',
    name: 'AfterManage',
    meta: { title: '投后管理', icon: 'clipboard', roles: ['admin'] },
    children: [
      {
        path: '/afterManage/combination',
        component: () => import('@/views/afterManage/combination/index'),
        name: 'AfterManageCombination',
        meta: { title: '投资分组', roles: ['admin'], noCache: true },
      },
      {
        path: '/afterManage/combination/details',
        component: () => import('@/views/afterManage/combination/details'),
        name: 'AfterManageCombinationDetails',
        meta: { title: '投资组合详情', roles: ['admin'], activeMenu: '/afterManage/combination', noCache: true },
        hidden: true
      },
      {
        path: '/afterManage/afterInvest',
        component: () => import('@/views/afterManage/afterInvest/index'),
        name: 'AfterInvest',
        meta: { title: '投资管理', roles: ['admin'], noCache: true }
      },
      {
        path: '/afterManage/afterInvest/detailsPage',
        component: () => import('@/views/afterManage/afterInvest/detailsPage'),
        name: 'AfterInvestDel',
        meta: { title: '净值分析', roles: ['admin'], activeMenu: '/afterManage/afterInvest', noCache: true },
        hidden: true
      },
      {
        path: '/afterManage/afterInvest/detailsPage/strategyAnalyse',
        component: () => import('@/views/afterManage/afterInvest/strategyAnalyse'),
        name: 'StrategyAnalyse',
        meta: { title: '子投资净值分析', roles: ['admin'], activeMenu: '/afterManage/afterInvest', noCache: true },
        hidden: true
      },
      {
        path: '/afterManage/afterInvest/add',
        component: () => import('@/views/afterManage/afterInvest/add'),
        name: 'AfterInvestAdd',
        meta: { title: '添加投资', roles: ['admin'], activeMenu: '/afterManage/afterInvest', noCache: true },
        hidden: true,
      },
      {
        path: '/afterManage/afterInvest/update',
        component: () => import('@/views/afterManage/afterInvest/add'),
        name: 'AfterInvestAdd',
        meta: { title: '编辑投资', roles: ['admin'], activeMenu: '/afterManage/afterInvest', noCache: true },
        hidden: true,
      },
      {
        path: '/afterManage/afterInvest/relation',
        component: () => import('@/views/afterManage/afterInvest/relation/index'),
        name: 'AfterInvestRelation',
        meta: { title: '子投资', roles: ['admin'], activeMenu: '/afterManage/afterInvest', noCache: true },
        hidden: true,
      },
      {
        path: '/afterManage/afterInvest/relation/detailsPage',
        component: () => import('@/views/afterManage/afterInvest/relation/detailsPage'),
        name: 'AfterInvestRelationDel',
        meta: { title: '编辑子投资', roles: ['admin'], activeMenu: '/afterManage/afterInvest', noCache: true },
        hidden: true,
      },
    ]
  },

  {
    path: '/setting',
    component: Layout,
    redirect: '/setting/indexPoll',
    name: 'Setting',
    meta: { title: '系统配置', roles: ['admin'], icon: 'el-icon-setting' },
    children: [
      {
        path: '/indexPoll',
        component: () => import('@/views/setting/indexPoll/index'),
        name: 'IndexPoll',
        meta: { title: '指数池', roles: ['admin'], noCache: true }
      },
      {
        path: '/indexPoll/indexPollDetails',
        component: () => import('@/views/setting/indexPoll/indexPollDetails'),
        name: 'IndexPollDetails',
        meta: { title: '指数分析', roles: ['admin'], activeMenu: '/indexPoll', noCache: true },
        hidden: true,
      },
      {
        path: '/attribute',
        component: () => import('@/views/setting/attribute/index'),
        name: 'Attribute',
        meta: { title: '策略属性', roles: ['admin'], noCache: true }
      },
    ]
  },

  {
    path: '/userPermission',
    component: Layout,
    name: 'UserPermission',
    meta: { title: '用户管理', roles: ['admin'] },
    children: [
      {
        path: '',
        component: () => import('@/views/userPermission'),
        name: 'UserPermission',
        meta: { title: '用户管理', roles: ['admin'], icon: 'peoples' }
      }
    ]
  },
  // {
  //   path: '/icon',
  //   component: Layout,
  //   children: [
  //     {
  //       path: 'index',
  //       component: () => import('@/views/icons/index'),
  //       name: 'Icons',
  //       meta: { title: 'Icons', icon: 'icon', noCache: true }
  //     }
  //   ]
  // },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  // routes: constantRoutes
  routes: [
    ...constantRoutes,
    ...asyncRoutes
  ]
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
